using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DragonAwakening.Core.Game;

namespace DragonAwakening.Game
{
    public class FightStatus
    {
        public FightStatus(List<List<Creature>> groups, List<KeyValuePair<string, float>> messageList)
        {
            this.currentGroups = groups;
            this.messageList = messageList;
        }

        public List<List<Creature>> currentGroups;

        public List<Creature> killedCreatures;

        private List<KeyValuePair<string, float>> messageList;

        private List<CombatCreature> combatOrder;

        private int combatIndex = 0;

        private GameInstance game;

        public void Initialize(GameInstance game)
        {
            this.messageList.Add(new KeyValuePair<string, float>("You have been attacked by the group of creatures!", 4.0F));
            var initiatives = new List<KeyValuePair<int, CombatCreature>>();
            foreach (var group in currentGroups)
            {
                foreach (var creature in group)
                {
                    var initiative = creature.Speed.Current + Dice.Roll(20);
                    initiatives.Add(new KeyValuePair<int, CombatCreature>(initiative, creature));
                }
            }

            foreach(var character in game.Group)
            {
                var initiative = character.Speed.Current + Dice.Roll(20);
                initiatives.Add(new KeyValuePair<int, CombatCreature>(initiative, character));
            }

            // Sort
            this.combatOrder = initiatives.OrderBy(x => x.Key).Select(x => x.Value).ToList();
            this.combatIndex = 0;
            this.game = game;
        }

        public void ExecuteStep()
        {
            var current = this.combatOrder[this.combatIndex];
            if (current.Turn(this.game, messageList))
            { 
                this.combatIndex = (this.combatIndex + 1) % this.combatOrder.Count;
            }
        }
    }
}
